import pymysql

# 在进行增删改，执行修改数据库的操作后不立即生效，使用连接conn.commit()提交后才生效，支持事物及回滚
db_config = {
      "host":"119.29.78.234",
       "port":3306,
       "user":"root",
       "password":"dhcc@2020",
       "db":"test123"
}
db = pymysql.connect(**db_config)
cursor = db.cursor()
sql = "INSERT INTO userinfo(username,passwd) VALUES('克莱','123')"
#sql = "UPDATE userinfo SET username = '奥尼尔' WHERE username = '科比'"  # 修改数据
#sql = "DELETE FROM username WHERE username ='奥尼尔'"                    # 删除数据
try:
    cursor.execute(sql)
    db.commit()
except Exception as e :   # 执行异常回滚
    db.rollback()
cursor.close()
db.close()
#或者在execute提供需要插入的数据
import pymysql
db_config = {
      "host":"119.29.78.234",
       "port":3306,
       "user":"root",
       "password":"dhcc@2020",
       "db":"test123"
}
db = pymysql.connect(**db_config)
cursor = db.cursor()
sql = "INSERT INTO userinfo(username,passwd) VALUES(%s,%s)"
try:
    cursor.execute(sql,("克莱","123"))
    db.commit()
except Exception as e :
    db.rollback()
cursor.close()
db.close()
#批量插入数据
import pymysql
db_config = {
      "host":"119.29.78.234",
       "port":3306,
       "user":"root",
       "password":"dhcc@2020",
       "db":"test123"
}
db = pymysql.connect(**db_config)
cursor = db.cursor()
sql = "INSERT INTO userinfo(username,passwd) VALUES(%s,%s)"
try:
    cursor.executemany(sql,[("韦德","123"),("字母哥","123")])
    db.commit()
except Exception as e :
    db.rollback()
cursor.close()
db.close()